package org.indp.mongowl.ui;

import com.vaadin.ui.Button;
import com.vaadin.ui.Component;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
import org.indp.mongowl.services.ServerConnection;

/**
 *
 */
public class DatabaseWorkspaceComponent extends VerticalLayout {

    private final ServerConnection serverConnection;
    private final String database;

    public DatabaseWorkspaceComponent(ServerConnection serverConnection, String database) {
        this.serverConnection = serverConnection;
        this.database = database;

        setSizeFull();
        Component workspace = createWorkspace();
        addComponents(createHeader(), workspace);
        setExpandRatio(workspace, 1);
    }

    private Component createWorkspace() {
        return new DatabaseBrowserComponent(serverConnection, database);
    }

    private Component createHeader() {
        return new Button("Home", new Button.ClickListener() {
            @Override
            public void buttonClick(Button.ClickEvent event) {
                ConnectionSelectorComponent.setup(getUI());
            }
        });
    }

    public static void setup(UI ui, ServerConnection serverConnection, String database) {
        DatabaseWorkspaceComponent component = new DatabaseWorkspaceComponent(serverConnection, database);
        ui.setContent(component);
        ui.getPage().setTitle(String.format("%s @ %s", database, serverConnection.getConnectionProfile().getName()));
    }
}
